"""
所有重命名的入口
"""
import os
import re
import time


def do_rename_feq(equipment_id, process_id, process_name, lot_number, product_type, operater, filepaths):
    pattern = re.compile(r'(\d*-\d*)\.xls')
    for filepath in filepaths:
        if filepath.endswith(".xls"):
            file_result = re.search(pattern, filepath)
            if file_result:
                print(file_result.group(1))
                # print(XinHaoQuery(file_result.group(1)))
                filedir = os.path.dirname(filepath)
                ModifiedTime = os.path.getmtime(filepath)  # 输出文件最近修改时间
                time_tuple = time.localtime(ModifiedTime)
                y = time.strftime('%Y', time_tuple)
                m = time.strftime('%m', time_tuple)
                d = time.strftime('%d', time_tuple)
                H = time.strftime('%H', time_tuple)
                M = time.strftime('%M', time_tuple)
                S = time.strftime('%S', time_tuple)
                print(y, m, d, H, M, S)
                ModifiedTimeName = y + "-" + m + "-" + d + "-" + H + "-" + M + "-" + S

                os.rename(filepath,
                          os.path.join(filedir,
                                       lot_number + "_"
                                       + product_type + "_"
                                       + process_id + "_"
                                       + process_name + "_"
                                       + equipment_id + "_"
                                       + file_result.group(1) + "_" + operater + "_"
                                       + ModifiedTimeName + ".xls"))


def do_rename_angle(equipment_id, process_id, process_name, lot_number, product_type, operater, filepaths):
    for filepath in filepaths:
        # print(file)
        if filepath.endswith(".csv"):
            print(filepath)
            filedir = os.path.dirname(filepath)
            ModifiedTime = os.path.getmtime(filepath)  # 输出文件最近修改时间
            time_tuple = time.localtime(ModifiedTime)
            y = time.strftime('%Y', time_tuple)
            m = time.strftime('%m', time_tuple)
            d = time.strftime('%d', time_tuple)
            H = time.strftime('%H', time_tuple)
            M = time.strftime('%M', time_tuple)
            S = time.strftime('%S', time_tuple)
            print(y, m, d, H, M, S)
            modified_time_name = y + "-" + m + "-" + d + "-" + H + "-" + M + "-" + S

            os.rename(filepath,
                      os.path.join(filedir,
                                   lot_number + "_"
                                   + product_type + "_"
                                   + process_id + "_" +
                                   process_name + "_"
                                   + equipment_id + "_"
                                   + operater + "_"
                                   + modified_time_name + ".csv"))


def do_rename_dimension(equipment_id, process_id, process_name, lot_number, product_type, operater, filepaths):
    for filepath in filepaths:
        # print(file)
        if filepath.endswith(".xlsx"):
            print(filepath)
            filedir = os.path.dirname(filepath)
            ModifiedTime = os.path.getmtime(filepath)  # 输出文件最近修改时间
            time_tuple = time.localtime(ModifiedTime)
            y = time.strftime('%Y', time_tuple)
            m = time.strftime('%m', time_tuple)
            d = time.strftime('%d', time_tuple)
            H = time.strftime('%H', time_tuple)
            M = time.strftime('%M', time_tuple)
            S = time.strftime('%S', time_tuple)
            print(y, m, d, H, M, S)
            ModifiedTimeName = y + "-" + m + "-" + d + "-" + H + "-" + M + "-" + S

            os.rename(filepath,
                      os.path.join(filedir,
                                   lot_number + "_"
                                   + product_type + "_"
                                   + process_id + "_"
                                   + process_name + "_"
                                   + equipment_id + "_"
                                   + operater + "_"
                                   + ModifiedTimeName + ".xlsx"))


def do_rename_files(equipment_id, process_id, process_name, lot_number, product_type, operater, filepaths):
    """实现重命名
    如果成功，返回None
    如果不成功，返回错误信息

    工序号	工序名称	设备名称
    101	频率及△F测试	MT-01
    102	直角度测试	CA-13
    103	尺寸测试	DS-01

    """
    try:
        if process_id == "101":
            do_rename_feq(equipment_id, process_id, process_name, lot_number, product_type, operater, filepaths)
        elif process_id == "102":
            do_rename_angle(equipment_id, process_id, process_name, lot_number, product_type, operater, filepaths)
        elif process_id == "103":
            do_rename_dimension(equipment_id, process_id, process_name, lot_number, product_type, operater, filepaths)
        return None
    except Exception as e:
        return str(e)
